package com.ysstech.common.util.verifyutil;


import java.util.HashMap;
import java.util.Random;

import com.ysstech.common.enums.YssCodeEnum;
import com.ysstech.common.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class VerifyCode {
    private final static HashMap<String, String> sensitiveKeyWords = new HashMap<String, String>();

    public VerifyCode() {

    }

    /**
     * 生成验证码
     *
     * @param type
     * @return
     */
    public static VerifyCodeInfo generateVerifyCode(Integer type) throws Exception {
        String srand = null;
        VerifyCodeInfo verifyInfo = null;
        YssCodeEnum yssCodeEnum = YssCodeEnum.getEnum(type);
        if (null == yssCodeEnum) {
            throw new BusinessException("没有获取到对应的验证码类型！");
        }
        Random initRandom = new Random();
        Random random = new Random(initRandom.nextLong());
        String rand = "";
        StringBuffer bRand = new StringBuffer();
        long verifyCodeTimeout = Long.valueOf(YssCodeEnum.TIMEOUT.getCode()) * 1000;
        switch (yssCodeEnum) {
            case ONE: /**生成6位纯数字手机验证码*/
                String sNumList = "0123456789";
                int numlen = sNumList.length();
                for (int i = 0; i < 6; i++) {
                    rand = String.valueOf(sNumList.charAt(random.nextInt(numlen)));
                    bRand.append(rand);
                }
                srand = bRand.toString();
                verifyInfo = new VerifyCodeInfo(srand, verifyCodeTimeout);
                break;
            case TWO: /**生成6位包含数字和字母组合验证码*/
                String sList = "2345678abcdefhijknprstuvxyz";
                int len = sList.length();
                for (int i = 0; i < 6; i++) {
                    rand = String.valueOf(sList.charAt(random.nextInt(len)));
                    bRand.append(rand);
                }
                srand = bRand.toString();
                srand = bRand.toString();
                verifyInfo = new VerifyCodeInfo(srand, verifyCodeTimeout);
                break;
            case THREE: /**生成4位数字和字母组合验证码,根据需求，为防止出现shit、fuck等关键字，4位字符中要至少包含一个数字*/
                String sList2 = "2345678abcdefhijknprstuvxyz";
                int len2 = sList2.length();
                String lastRand = "";
                String number = String.valueOf(sList2.charAt(random.nextInt(7)));
                int numposition = random.nextInt(4);
                for (int i = 0; i < 4; i++) {
                    if (i == numposition) {
                        bRand.append(number);
                    } else {
                        lastRand = rand;
                        rand = String.valueOf(sList2.charAt(random.nextInt(len2)));
                        while (checkSensitiveKeyWord(lastRand, rand)) {
                            rand = String.valueOf(sList2.charAt(random.nextInt(len2)));
                        }
                        bRand.append(rand);
                    }
                }
                srand = bRand.toString();
                verifyInfo = new VerifyCodeInfo(srand, verifyCodeTimeout);
                break;
            case FOURE:    /**生成4位纯数字手机验证码*/
                String sNumList2 = "0123456789";
                int numlen2 = sNumList2.length();
                for (int i = 0; i < 4; i++) {
                    rand = String.valueOf(sNumList2.charAt(random.nextInt(numlen2)));
                    bRand.append(rand);
                }
                srand = bRand.toString();
                verifyInfo = new VerifyCodeInfo(srand, verifyCodeTimeout);
                break;
            default:
                throw new BusinessException("Unexpected valueMoney: " + yssCodeEnum.getEnum(type));
        }
        return verifyInfo;
    }

    /**
     * 检查keyword2是否不应该和keyword1连续出现
     *
     * @param keyword1
     * @param keyword2
     * @return
     */
    public static boolean checkSensitiveKeyWord(String keyword1, String keyword2) {
        if (keyword2.equals(sensitiveKeyWords.get(keyword1)))
            return true;
        else
            return false;
    }

}
